/* 封装 axios 请求方法 */

import service from './service'
import { Get, Post } from './type'
// import qs from 'qs'
// import fileDownload from "js-file-download";

// 如果get传参 在config.params中添加
const get: Get = async (url, config) => {
  const response = await service.get(url, { ...config })
  return response
}

const post: Post = async (url, data, config) => {
  const response = await service.post(url, data, { ...config })
  return response
}

const get_down = async (url: string, params: any) => {
  return new Promise((resolve, reject) => {
    service
      .get(url, {
        params: params,
        // paramsSerializer: (params) => {
        //   return qs.stringify(params, {
        //     indices: false,
        //   })
        // },
        responseType: 'blob',
      })
      .then((res) => {
        let fileName = decodeURIComponent(
          res.headers['content-disposition'].split(`UTF-8`)[1]
        ).slice(2)
        // fileDownload(res.data, fileName);
        resolve(res)
      })
      .catch((err) => {
        reject
      })
  })
}
const post_down = async (url: string, data: any) => {
  const response = await service
    .post(url, data, {
      responseType: 'arraybuffer',
    })
    .then((res) => {
      let fileName = decodeURIComponent(res.headers['content-disposition'].split(`UTF-8`)[1]).slice(
        2
      )
      // fileDownload(res.data, fileName);
    })
  return response
}
const request = {
  get,
  post,
  get_down,
  post_down,
}

export default request
